﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DataImporter.Models;
using System.Text.RegularExpressions;

namespace DataImporter.Modules
{
    public partial class ucTJZBPG : UserControl
    {
        public ucTJZBPG()
        {
            InitializeComponent();
        }

        private List<Excel> excelList = new List<Excel>();

        private void btnAnalyze_Click(object sender, EventArgs e)
        {
            excelList.Clear();
            excelList.AddRange(this.rtbContent.Text.ToExcelList());
            excelList.ForEach(item =>
            {
                item.E = Regex.Replace(item.E, "[a-zA-Z-]{3,}", "v");
                item.E = item.E.AnalizeCondition();
                item.F = item.F.AnalizeCondition();
                item.G = item.G.AnalizeCondition();
                item.J = item.J.AnalizeExplainCondition();
            });
            this.dgvData.DataSource = excelList;
        }

        private void btnGetAllData_Click(object sender, EventArgs e)
        {
            //this.dgvData.DataSource = DBContext.SimpleClient.GetList<NCD_LTHR>();
        }

        private void btnSaveTJXM_Click(object sender, EventArgs e)
        {
            //this.dgvData.DataSource = DBContext.SimpleClient.GetList<NCD_LTHRReference>();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var dcn = new Dictionary<LTHR, List<NCD_LTHRReference>>();
            var order = 0;
            var childOrder = 0;
            foreach (var item in excelList)
            {
                if (!item.A.IsNullOrEmpty())
                {
                    dcn.Add(new LTHR
                    {
                        Name = item.B.Trim(),
                        ShortName = item.C.Trim(),
                        Unit = item.D.Trim(),
                        Code = item.A.Trim(),
                        Category = item.B.Trim(),
                        Order = order += 5
                    }, new List<NCD_LTHRReference>());
                }
                var f = new NCD_LTHRReference
                {
                    NLR_ExceptionExplain = item.J.Trim(),
                    NLR_Order = childOrder += 5,
                    NLR_RiskLevel = item.H.Trim(),
                    NLR_Score = int.Parse(item.I.Trim()),
                };
                if (!item.E.IsNullOrEmpty())
                {
                    //通用
                    f.NLR_Standard = item.E.Trim();
                    dcn.Last().Value.Add(f);
                }
                else
                {
                    //不是通用的
                    f.NLR_Standard = item.F.Trim(); //男
                    f.NLR_Regular = "'sex'=='男'";
                    dcn.Last().Value.Add(f);
                    f = f.Clone();
                    f.NLR_Order = childOrder += 5;
                    f.NLR_Standard = item.G.Trim(); //女
                    f.NLR_Regular = "'sex'=='女'";
                    dcn.Last().Value.Add(f);
                }
            }
            foreach (var item in dcn)
            {
                //var id = DBContext.SimpleClient.InsertReturnIdentity(item.Key);
                //item.Value.ForEach(c => c.NLR_NLID = id);
                //DBContext.SimpleClient.InsertRange(item.Value);
            }
            MessageBox.Show("保存成功");
        }
    }
}
